package com.example.test;


import java.net.URLClassLoader;

/*
    内部包装一个
 */
public class App {

    //启动的状态
    /*
        0 表示尚未启动
        1 表示正在启动
        2 表示成功启动
        3 表示启动失败
        4 表示正在终止
        5 表示已被成功终止
     */
    private transient int status = 0;

    private URLClassLoader classLoader;


    //上下文对象
    private Object context;

    private Exception exception;

    void setClassLoader(URLClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    public URLClassLoader getClassLoader() {
        return classLoader;
    }

    void setException(Exception exception) {
        this.exception = exception;
    }

    public Exception getException() {
        return exception;
    }

    void setContext(Object context) {
        this.context = context;
    }

    public void stop() {
        new Thread(() -> {
            try {
                context.getClass().getMethod("stop").invoke(context);
                context.getClass().getMethod("close").invoke(context);

                while (true) {
                    Thread.sleep(200);
                    Boolean isActive = (Boolean) context.getClass().getMethod("isActive").invoke(context);
                    if (!isActive) {
                        break;
                    }
                }
                status = 5;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }).start();
        status = 4;

    }

    void setStatus(int status) {
        this.status = status;
    }

    public int getStatus() {
        return status;
    }
}
